﻿using System;
using System.ComponentModel;
using Loki.Utils;
using Luna.Model.Budget;

namespace Luna.UI.Budget
{
    public class BudgetEditViewModel : LunaViewModel
    {
        public BudgetEditViewModel()
        {
            Income = CreateTrackedCollection<DisplayableBudgetLine>();
            OutCome = CreateTrackedCollection<DisplayableBudgetLine>();

            Init += BudgetEditViewModel_Init;
            Load += BudgetEditViewModel_Load;
        }

        #region Name

        private static PropertyChangedEventArgs _NameChangedArgs = ObservableHelper.CreateChangedArgs<BudgetEditViewModel>(x => x.Name);

        private static PropertyChangingEventArgs _NameChangingArgs = ObservableHelper.CreateChangingArgs<BudgetEditViewModel>(x => x.Name);

        private string _Name;

        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                if (value != _Name)
                {
                    NotifyChanging(_NameChangingArgs);
                    _Name = value;
                    NotifyChangedAndDirty(_NameChangedArgs);
                }
            }
        }

        #endregion Name

        #region CurrentBudget

        private static PropertyChangedEventArgs _CurrentBudgetChangedArgs = ObservableHelper.CreateChangedArgs<BudgetEditViewModel>(x => x.CurrentBudget);

        private static PropertyChangingEventArgs _CurrentBudgetChangingArgs = ObservableHelper.CreateChangingArgs<BudgetEditViewModel>(x => x.CurrentBudget);

        private IEditableBudget _CurrentBudget;

        public IEditableBudget CurrentBudget
        {
            get
            {
                return _CurrentBudget;
            }

            set
            {
                if (value != _CurrentBudget)
                {
                    NotifyChanging(_CurrentBudgetChangingArgs);
                    _CurrentBudget = value;
                    NotifyChanged(_CurrentBudgetChangedArgs);
                }
            }
        }

        #endregion CurrentBudget

        public LokiCollection<DisplayableBudgetLine> Income { get; private set; }

        public IBudget OldVersion { get; set; }

        public LokiCollection<DisplayableBudgetLine> OutCome { get; private set; }

        private void Budget_Changed(object sender, System.EventArgs e)
        {
            if (CurrentBudget != null)
            {
                Name = CurrentBudget.Name;
            }
        }

        private void BudgetEditViewModel_Init(object sender, System.EventArgs e)
        {
            this.WatchChange(this, x => x.Name, c => c.Name_Changed);
            this.WatchChange(this, x => x.CurrentBudget, c => c.Budget_Changed);
        }

        private void BudgetEditViewModel_Load(object sender, System.EventArgs e)
        {
            Income.Clear();

            foreach (var L_Line in CurrentBudget.Lines.Values)
            {
                var L_Display = new DisplayableBudgetLine() { ID = L_Line.ID, Code = L_Line.Code, Nom = L_Line.Name, ParentID = L_Line.Parent != null ? L_Line.Parent.ID : Guid.Empty };
                Income.Add(L_Display);
            }
            /*DisplayableBudgetLine L_C1 = new DisplayableBudgetLine() { Code = "1", Nom = "Comptes de capitaux" };
            DisplayableBudgetLine L_C10 = new DisplayableBudgetLine() { Code = "10", Nom = "Capital et réserves", ParentID = L_C1.ID };
            DisplayableBudgetLine L_C101 = new DisplayableBudgetLine() { Code = "101", Nom = "Capital", ParentID = L_C10.ID };
            DisplayableBudgetLine L_C102 = new DisplayableBudgetLine() { Code = "102", Nom = "Primes liées au capital", ParentID = L_C10.ID };

            Income.AddRange(new[] { L_C1, L_C10, L_C101, L_C102 });*/
        }

        private void Name_Changed(object sender, System.EventArgs e)
        {
            DisplayName = Name;
        }
    }
}